package com.web.portal.controller;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


import com.web.portal.entity.AcademicPrograms;
import com.web.portal.entity.AdvisorMeeting;
import com.web.portal.entity.Faculty;
import com.web.portal.entity.Lab;
import com.web.portal.entity.LabPolicy;
import com.web.portal.entity.TransferCredit;
import com.web.portal.entity.TutoringCenter;
import com.web.portal.exception.PortalException;
import com.web.portal.mail.MailNotificationImpl;
import com.web.portal.pojo.EmailPojo;
import com.web.portal.service.AcademicProgramsService;
import com.web.portal.service.AdvisorMeetingService;
import com.web.portal.service.FacultyService;
import com.web.portal.service.LabPolicyService;
import com.web.portal.service.LabService;
import com.web.portal.service.TransferCreditService;
import com.web.portal.service.TutoringCenterService;

@Controller
public class StudentViewController {
	/*
	 * view faculty and staff
	 */
	private static final Logger LOG = Logger.getLogger(StudentViewController.class);
	@Autowired
	FacultyService service;

	@RequestMapping(value = "/facultystudent.do")
	public String facultyLists(
			Map<String, Object> map,
			@RequestParam(value = "student", required = false) boolean isStudent,
			HttpServletRequest request) throws PortalException {

		Faculty faculty = new Faculty();
		List<Faculty> facultyList = service.getList(faculty);

		/*
		 * sort base don first name, if equal first name than last name
		 */
		Collections.sort(facultyList);
		map.put("facultyList", facultyList);
		
			return "faculty_and_staff_view_for_students";

		}

	
@RequestMapping(value = "/viewfacultyforstudent.do", method = RequestMethod.GET)
public String viewFacultyOrStaff(@RequestParam("email") final String email,
		Map<String, Object> map) throws PortalException {
	if (!(email == null)) {
		List<Faculty> thisFaculty = new ArrayList<Faculty>();
		Faculty faculty = service.getFacultyImageOrMapFile(email);
		thisFaculty.add(faculty);
		map.put("faculty", thisFaculty);
		return "faculty_view_student";
	}
	return null;
}
@RequestMapping(value = "/getlabforstudent.do", method = RequestMethod.GET)
public String getLabManagerPage(Map<String, Object> model) {

	return "lab_student";

}
@Autowired
LabService labService;

public LabService getLabService() {
	return labService;
}

public void setLabService(LabService labService) {
	this.labService = labService;
}

@RequestMapping(value = "/getlabinfostudent.do", method = RequestMethod.GET)
public String getLabInfo(Map<String, Object> model) throws PortalException {
	Lab lab = new Lab();
	List<Lab> labInfos = labService.getList(lab);
	Lab labs=null;
	if(!labInfos.isEmpty()){
		labs=labInfos.get(0);
	}
	List<Lab>labInfo= new ArrayList<Lab>();
	labInfo.add(labs);
	model.put("labs", labInfo);

	return "lab_student_view";

}
@Autowired
LabPolicyService labPolicyService;

public LabPolicyService getLabPolicyService() {
	return labPolicyService;
}

public void setLabPolicyService(LabPolicyService labPolicyService) {
	this.labPolicyService = labPolicyService;
}

@RequestMapping(value = "/labpolicystudent.do")
public String getLabPolicy(Map<String, Object> model) throws PortalException {
	LabPolicy labPolicy = new LabPolicy();
	List<LabPolicy> labPolicyList = labPolicyService.getList(labPolicy);
	LabPolicy labP=null;
	if(!labPolicyList.isEmpty()){
		labP=labPolicyList.get(0);
	}
	List<LabPolicy>  labPolicyLists= new ArrayList<LabPolicy>();
	labPolicyLists.add(labP);
	model.put("labPolicyList", labPolicyLists);

	return "lab_policy_student_view";
}
@Autowired
TutoringCenterService tutoringCenterService;

public TutoringCenterService getTutoringCenterService() {
	return tutoringCenterService;
}

public void setTutoringCenterService(TutoringCenterService tutoringCenterService) {
	this.tutoringCenterService = tutoringCenterService;
}
@RequestMapping (value="/gettutoringcenterstudent.do")
public String getListOfTutoringCenter(Map<String,Object>model) throws PortalException{
	TutoringCenter tutoringCenter= new TutoringCenter();
	List<TutoringCenter> tutoringCenterLists =tutoringCenterService.getList(tutoringCenter);
	TutoringCenter tutoringCent=null;
	if(!tutoringCenterLists.isEmpty()){
		tutoringCent=tutoringCenterLists.get(0);
	}
	List<TutoringCenter>  tutoringCenterList= new ArrayList<TutoringCenter>();
	tutoringCenterList.add(tutoringCent);
	model.put("tutoringCenterList", tutoringCenterList);

	return "tutoring_center_student_view";
}
@Autowired
AdvisorMeetingService advisorMeetingService;

public AdvisorMeetingService getAdvisorMeetingService() {
	return advisorMeetingService;
}

public void setAdvisorMeetingService(AdvisorMeetingService advisorMeetingService) {
	this.advisorMeetingService = advisorMeetingService;
}
@RequestMapping(value="/advisormeetingstudent.do")
public String getAdvisorMeeting(Map<String, Object> model) throws PortalException{
	AdvisorMeeting advisorMeeting= new AdvisorMeeting();
	List<AdvisorMeeting > advisorMeetingLists= advisorMeetingService.getAllAdvisorMeeting(advisorMeeting);
	AdvisorMeeting a=null;
	if(!advisorMeetingLists.isEmpty()){
		a=advisorMeetingLists.get(0);
	}
	List<AdvisorMeeting>advisorMeetingList= new ArrayList<AdvisorMeeting>();
	advisorMeetingList.add(a);
	model.put("advisorMeetingList", advisorMeetingList);
	LOG.debug("returning advisor meeting page...........................");

	return "advisor_meeting_student_view";
}
@Autowired
AcademicProgramsService academicProgramsService;

public AcademicProgramsService getAcademicProgramsService() {
	return academicProgramsService;
}

public void setAcademicProgramsService(
		AcademicProgramsService academicProgramsService) {
	this.academicProgramsService = academicProgramsService;
}
@RequestMapping (value="/getacademicprogramsstudent.do")
public String getAcademicPrograms (Map<String, Object> model) throws PortalException{
	AcademicPrograms academicPrograms= new AcademicPrograms();
	List<AcademicPrograms> programLists=academicProgramsService.getList(academicPrograms);
	AcademicPrograms a=null;
	if(!programLists.isEmpty()){
		a=programLists.get(0);
	}
	List<AcademicPrograms> programList= new ArrayList<AcademicPrograms>();
	programList.add(a);
	model.put("programList", programList);

	return "academic_programs_students_page";
}
@Autowired
TransferCreditService transferCreditService;

public TransferCreditService getTransferCreditService() {
	return transferCreditService;
}

public void setTransferCreditService(TransferCreditService transferCreditService) {
	this.transferCreditService = transferCreditService;
}
@RequestMapping (value="/gettransfercreditsstudents.do")
public String getTransferCredits (Map<String, Object> model) throws PortalException{
	TransferCredit transferCredits= new TransferCredit();
	List<TransferCredit> listTransferCredit=transferCreditService.getList(transferCredits);
	/*
	 * just put one
	 */
	TransferCredit credit=null;
	if(!listTransferCredit.isEmpty()){
	 credit=listTransferCredit.get(0);
	}
	List<TransferCredit>  listTransferCredits= new ArrayList<TransferCredit>();
	listTransferCredits.add(credit);
	model.put("listTransferCredits", listTransferCredits);
	return "transfer_credit_student_page";
}
@Autowired
MailNotificationImpl mailNotification;
@RequestMapping (value="/email.do", method=RequestMethod.POST)
public String getTransferPage(@Valid @ModelAttribute("emailPojo")final EmailPojo emailPojo, BindingResult bindingResult, Map model) throws PortalException{
	model.put("errors", bindingResult.getAllErrors());
	String to =emailPojo.getEmail();
	String subject=" Transfer Link to Towson University ";
	TransferCredit transferCredits= new TransferCredit();
	List<TransferCredit> listTransferCredit=transferCreditService.getList(transferCredits);
	String link=listTransferCredit.get(0).getLinkToTransferCreditsPage();
	
	String body = "<html><head></head><body>Please click the link below  to get the transfer information :"+
	"<br/><a href=\""+link+"\">"+"Link To Transfer page </a>"+
	"</body></html>";
	
	mailNotification.sendEmail(to, subject, body);
	LOG.debug("Send email successfully...............................");
	return "redirect:/gettransfercreditsstudents.do";
	
}

}



